const app = getApp()
import {
    modifyPortrait
} from '../../api/app'
import {
    getWaitingList
} from '../../api/get-paid.js'
import user from '../../utils/user'
import {
    goApp
} from '../../utils/util'
import global from '../../utils/global'
Component({
    data: {
        top: app.globalData.menuTop,
        isAvatarTips: false,
        userName: '',
        avatar: '',
        totalRevenue: 0,
        toBePaid: 0,
    },
    pageLifetimes: {
        show() {
            this.getUserInfo()
            if (wx.getStorageSync('isAvatarTips') === '1') {
                this.setData({
                    isAvatarTips: false
                })
            } else {
                this.setData({
                    isAvatarTips: true
                })
            }
        }
    },
    created() {
        this.getUserInfo()
    },
    methods: {
        getUserInfo() {
            user.waitLogin().then(() => {
                this.setData({
                    avatar: user.avatarUrl,
                    userName: user.userName,
                })
                console.log(this.data.avatar, '头像url')
                getWaitingList({}).then(res => {
                    let num = 0
                    res.record.forEach((item) => {
                        num += item.paymentAmount * 100
                    })
                    this.setData({
                        totalRevenue: res.totalMoney,
                        toBePaid: num / 100
                    })
                })
            })
        },
        goPath(event) {
            if (!user.userId && event.currentTarget.dataset.path !== 'applet::/pages/salary/salary') {
                // app.confirm('您还未注册，是否立即前往注册？').then(() => {
                //     user.goRegister()
                // })
                return 0
            }
            goApp(event.currentTarget.dataset.path)
        },
        toPersonalMsg() {
            wx.setStorageSync('isAvatarTips', '1')
            this.setData({
                isAvatarTips: false
            })
            wx.navigateTo({
                url: '/pages/personal-info/personal-info',
            })
        },
        onModifyPortrait(event) {
            console.log(111)
            if (!user.userId) {
                console.log(222)
                return
            }
            wx.uploadFile({
                url: global.baseUrl + '/api/v1/blue/user/upload',
                filePath: event.detail.avatarUrl,
                name: 'file',
                success: (res) => {
                    modifyPortrait({
                        userHeadUrl: JSON.parse(res.data).data.fileUrl
                    }).then(() => {
                        app.toast('修改成功')
                        this.setData({
                            userHeadUrl: JSON.parse(res.data).data.fileUrl,
                            avatar: JSON.parse(res.data).data.fileUrl
                        })
                        user.avatarUrl = JSON.parse(res.data).data.fileUrl
                        console.log(this.data.userHeadUrl, 'userHeadUrl')
                    })
                },
                fail() {
                    console.log('failfail')
                }
            })
            // wx.getUserProfile({
            //     lang: 'zh_CN',
            //     desc: '向团队成员展示头像信息',
            //     success:(res)=> {
            //         console.log(res, 'resres')
            //         console.log(res,JSON.parse(res.rawData))
            //         modifyPortrait({
            //             userHeadUrl:res.userInfo.avatarUrl
            //         }).then(()=>{
            //             this.setData({
            //                 isAvatarTips:false
            //             })
            //             app.toast('修改成功')
            //             this.setData({
            //                 userHeadUrl:res.userInfo.avatarUrl
            //             })
            //         })
            //     },
            //     fail(err) {
            //         console.log(err)
            //     }
            // })
        },
        onClose() {
            wx.setStorageSync('isAvatarTips', '1')
            this.setData({
                isAvatarTips: false
            })
        },
        toLogin() {
            user.goRegister()
        }
    }
})